﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product;
using ERP_RORZE_PRODUCT.Product.BusOder;
using ERP_RORZE_DLL.Product.database;
using System.Data.SqlClient;
using ERP_RORZE_DLL;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using ERP_RORZE_DLL.Product.Base;
using DevExpress.XtraEditors.Repository;

namespace ERP_RORZE_PRODUCT.Product.UserControls
{
    public partial class ctrlBusOderList : DevExpress.XtraEditors.XtraUserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private SqlConnection masterConnection;

        //
        ClsBusOderDAO clsBusOderDAO = new ClsBusOderDAO();
        DataTable dt;
        ClsBusOderTable busoder = new ClsBusOderTable();

        public ctrlBusOderList()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.masterConnection = new SqlConnection(mySetting.DB2ConnectionString);
        }
        void Re_load(object sender, EventArgs e)
        {
            dt = clsBusOderDAO.GetAll();
            dgvBusOderList.DataSource = dt;
            gridViewBusOder.BestFitColumns();

            
                RepositoryItemTextEdit ri = new RepositoryItemTextEdit();
                DevExpress.XtraGrid.Columns.GridColumn col = gridViewBusOder.Columns["Imported"];
                col.ColumnEdit = ri;
                col.OptionsColumn.AllowEdit = false;
                ri.CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(ri_CustomDisplayText);

                RepositoryItemTextEdit ri1 = new RepositoryItemTextEdit();
                DevExpress.XtraGrid.Columns.GridColumn col1 = gridViewBusOder.Columns["Started"];
                col1.ColumnEdit = ri1;
                col1.OptionsColumn.AllowEdit = false;
                ri1.CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(ri_CustomDisplayText);

                RepositoryItemTextEdit ri2 = new RepositoryItemTextEdit();
                DevExpress.XtraGrid.Columns.GridColumn col2 = gridViewBusOder.Columns["Finished"];
                col2.ColumnEdit = ri2;
                col2.OptionsColumn.AllowEdit = false;
                ri1.CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(ri_CustomDisplayText);
            
        }
        private void ctrlBusOderList_Load(object sender, EventArgs e)
        {
            Re_load(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            busoder.BOderNo = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "BOderNo").ToString();
            clsBusOderDAO.Delete(busoder);
            Re_load(sender, e);
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            Re_load(sender, e);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            Frm_BusOderAdd frmAdd = new Frm_BusOderAdd();
            frmAdd.Re_Load += new EventHandler(Re_load);
            frmAdd.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                Frm_BusOderEdit frmEdit = new Frm_BusOderEdit();
                frmEdit.Re_Load += new EventHandler(Re_load);
                frmEdit.BOderNo = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "BOderNo").ToString();
                frmEdit.BlastQty = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "BlastQty").ToString();
                frmEdit.Change = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "Change").ToString();
                frmEdit.ChangeDate = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "ChangeDate").ToString();
                frmEdit.date = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "date").ToString();
                frmEdit.Dealine = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "Deadline").ToString();
                frmEdit.Finished = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "Finished").ToString();
                frmEdit.FinishDate = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "FinishDate").ToString();
                frmEdit.HelisertQty = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "HelisertQty").ToString();
                frmEdit.Imported = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "Imported").ToString();
                frmEdit.ImportFrom = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "ImportFrom").ToString();
                //frmEdit.MONo = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "MONo").ToString();
                frmEdit.MOQty = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "MOQty").ToString();
                frmEdit.Note = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "Note").ToString();
                frmEdit.PartID = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "PartID").ToString();
                frmEdit.planNo = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "planNo").ToString();
                frmEdit.Qty = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "Qty").ToString();
                frmEdit.RawQty = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "RawQty").ToString();
                frmEdit.Started = gridViewBusOder.GetRowCellValue(gridViewBusOder.FocusedRowHandle, "Started").ToString();
                frmEdit.ShowDialog();
            }
            catch
            {
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            FileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Excel 2007-2013|*.xlsx|Excel 97-2003 (*.xls)|*.xls";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                OleDbConnection connection = new OleDbConnection();
                try
                {
                    string path = fileDialog.FileName;
                    INISettings iniSettings = new INISettings();
                    string connectString = iniSettings.SQLConnectionString;
                    // Connection String to Excel Workbook
                    string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;Persist Security Info=False", path);

                    connection.ConnectionString = excelConnectionString;
                    OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
                    connection.Open();
                    // Create DbDataReader to Data Worksheet
                    DbDataReader dr = command.ExecuteReader();

                    // Bulk Copy to SQL Server 
                    SqlBulkCopy bulkInsert = new SqlBulkCopy(connectString);
                    bulkInsert.DestinationTableName = "[rrc_database].[dbo].[242_BusOder]";

                    SqlBulkCopyColumnMapping date = new SqlBulkCopyColumnMapping("BOderNo", "BOderNo");
                    bulkInsert.ColumnMappings.Add(date);

                    SqlBulkCopyColumnMapping toolId = new SqlBulkCopyColumnMapping("date", "date");
                    bulkInsert.ColumnMappings.Add(toolId);

                    SqlBulkCopyColumnMapping makerId = new SqlBulkCopyColumnMapping("planNo", "planNo");
                    bulkInsert.ColumnMappings.Add(makerId);

                    SqlBulkCopyColumnMapping PartID = new SqlBulkCopyColumnMapping("PartID", "PartID");
                    bulkInsert.ColumnMappings.Add(PartID);

                    SqlBulkCopyColumnMapping Qty = new SqlBulkCopyColumnMapping("Qty", "Qty");
                    bulkInsert.ColumnMappings.Add(Qty);

                    SqlBulkCopyColumnMapping Deadline = new SqlBulkCopyColumnMapping("Deadline", "Deadline");
                    bulkInsert.ColumnMappings.Add(Deadline);

                    SqlBulkCopyColumnMapping RawQty = new SqlBulkCopyColumnMapping("RawQty", "RawQty");
                    bulkInsert.ColumnMappings.Add(RawQty);

                    SqlBulkCopyColumnMapping HelisertQty = new SqlBulkCopyColumnMapping("HelisertQty", "HelisertQty");
                    bulkInsert.ColumnMappings.Add(HelisertQty);

                    SqlBulkCopyColumnMapping BlastQty = new SqlBulkCopyColumnMapping("BlastQty", "BlastQty");
                    bulkInsert.ColumnMappings.Add(BlastQty);

                    SqlBulkCopyColumnMapping MONo = new SqlBulkCopyColumnMapping("MONo", "MONo");
                    bulkInsert.ColumnMappings.Add(MONo);

                    SqlBulkCopyColumnMapping MOQty = new SqlBulkCopyColumnMapping("MOQty", "MOQty");
                    bulkInsert.ColumnMappings.Add(MOQty);

                    SqlBulkCopyColumnMapping Started = new SqlBulkCopyColumnMapping("Started", "Started");
                    bulkInsert.ColumnMappings.Add(Started);

                    SqlBulkCopyColumnMapping Finished = new SqlBulkCopyColumnMapping("Finished", "Finished");
                    bulkInsert.ColumnMappings.Add(Finished);

                    SqlBulkCopyColumnMapping FinishDate = new SqlBulkCopyColumnMapping("FinishDate", "FinishDate");
                    bulkInsert.ColumnMappings.Add(FinishDate);

                    SqlBulkCopyColumnMapping Change = new SqlBulkCopyColumnMapping("Change", "Change");
                    bulkInsert.ColumnMappings.Add(Change);

                    SqlBulkCopyColumnMapping ChangeDate = new SqlBulkCopyColumnMapping("ChangeDate", "ChangeDate");
                    bulkInsert.ColumnMappings.Add(ChangeDate);

                    SqlBulkCopyColumnMapping Imported = new SqlBulkCopyColumnMapping("Imported", "Imported");
                    bulkInsert.ColumnMappings.Add(Imported);

                    SqlBulkCopyColumnMapping ImportFrom = new SqlBulkCopyColumnMapping("ImportFrom", "ImportFrom");
                    bulkInsert.ColumnMappings.Add(ImportFrom);

                    SqlBulkCopyColumnMapping Note = new SqlBulkCopyColumnMapping("Note", "Note");
                    bulkInsert.ColumnMappings.Add(Note);

                    bulkInsert.WriteToServer(dr);

                    connection.Close();
                    Re_load(sender, e);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    connection.Close();
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Re_load(sender, e);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            btnImport_Click(sender, e);
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            btnEdit_Click(sender, e);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            btnDelete_Click(sender, e);
        }

        private void btnBusOderCancel_Click(object sender, EventArgs e)
        {
            CtrlBusOderCancel frmAdd = new CtrlBusOderCancel();
            frmAdd.Reload += new EventHandler(Re_load);
            frmAdd.ShowDialog();
        }

        private void btnBusOderDeadline_Click(object sender, EventArgs e)
        {
            ctrlBusOderDeadline frmObj = new ctrlBusOderDeadline();
            frmObj.Reload += new EventHandler(Re_load);
            frmObj.ShowDialog();
        }

        private void btnBusOderQtyChange_Click(object sender, EventArgs e)
        {
            ctrlBusOderQtyChange frmObj = new ctrlBusOderQtyChange();
            frmObj.Reload += new EventHandler(Re_load);
            frmObj.ShowDialog();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Frm_BusOderAdd frmAdd = new Frm_BusOderAdd();
            frmAdd.Re_Load += new EventHandler(Re_load);
            frmAdd.ShowDialog();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            clsConstantsFuction.ExportToExcel(gridViewBusOder, 1, true);
        }
        void ri_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            e.DisplayText = true.Equals(e.Value) ? "True" : "False";
        }
    }
}
